import * as moment from "moment";
import { BaseEntity } from "src/baseEntity";
import { UserSys } from "src/user/userSys.entity";
import { cryptoPassword } from "src/util/crypto";
import { Column, Entity, ManyToOne } from "typeorm";



@Entity("post")
export class Post extends BaseEntity {
    @ManyToOne(() => UserSys)
    user: UserSys

    plan_today: string

    @Column()
    work_today: string

    @Column()
    summary_today: string

    @Column()
    plan_tomorrow: string

    @Column({ unique: true })
    hash_key: string

    public static getCurrentHashKey(user: UserSys) {
        return Post.getHashKey(user, moment());
    }

    public static getYesterday(user: UserSys) {
        return Post.getHashKey(user, moment().subtract(1, "day"));
    }

    public static getHashKey(user: UserSys, time: moment.Moment) {
        const date = time.startOf("day").format("YYYY-MM-DD");
        return cryptoPassword(`${date}-${user.id}`);
    }
}